package com.fy.utils.tessdata;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class TesseractUtil {

    private static String tessPath = "/usr/bin";
    //private static String tessPath = "D:/workDep/installFolder/tesseract/Tesseract-OCR";

    private static String language = "chi_sim";

    private static  String city = "北京、上海、天津、重庆、黑龙江、辽宁、吉林、河北、河南、湖北、湖南、山东、山西、陕西、" +
            "安徽、浙江、江苏、福建、广东、海南、四川、云南、贵州、青海、" +
            "甘肃、江西、台湾、内蒙古、宁夏、新疆、西藏、广西、香港、澳门";


    public static String identifyTextFromPicture(String sourceFileName, String savePath) {

        ProcessBuilder pb = new ProcessBuilder();
        pb.directory(new File(tessPath));
        pb.redirectErrorStream(true);
        File sourceFile = new File(sourceFileName);
        List<String> cmd = new ArrayList<String>();
        String ocr_result_filename = sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf("."));
        cmd.add(pb.directory().getAbsolutePath() + File.separatorChar + "tesseract");
        cmd.add(sourceFile.getAbsolutePath());
        cmd.add(savePath + File.separatorChar + ocr_result_filename);
        cmd.add("-l");
        cmd.add(language);

        pb.command(cmd);
        try {
            Process process = pb.start();
            if (process.waitFor() == 0) {
                return savePath + File.separatorChar + ocr_result_filename + ".txt";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    public static String readImage(String sourceFileName, String savePath) {

        String resultFile = TesseractUtil.identifyTextFromPicture(sourceFileName, savePath);
        File file = new File(resultFile);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();

            //得到读取的内容
            String content = sbf.toString();
            content = content.replaceAll(" ","");

            System.out.println(content);


            String[] cityArray = city.split("、");
            List<Map<String,String>> cityAndIndex = new ArrayList<>();

            for (int i = 0; i < cityArray.length; i++) {

                while (content.indexOf(cityArray[i]) > -1){
                    Map<String,String> map = new HashMap<>();
                    String city = content.substring(content.indexOf(cityArray[i]),content.length());
                    String addCity =  city.substring(0,city.indexOf("市") + 1);
                    if(city.indexOf("市") < city.length() - 1){
                        String fuhao = city.substring(city.indexOf("市") + 1, city.indexOf("市") + 2);
                        if(fuhao.equals("*")){
                            addCity += "*";
                        }
                    }
                    map.put("index",content.indexOf(cityArray[i]) + "");
                    map.put("city",addCity);
                    cityAndIndex.add(map);

                    //替换掉出现过得省份和城市
                    String repStr = "";
                    for (int j = 0; j < addCity.length(); j++) {
                        repStr += "F";
                    }

                    content = content.replace(addCity,repStr);

                    //System.out.println(content);
                }


            }
            //排序
            Collections.sort(cityAndIndex, new Comparator<Map<String,String>>() {
                @Override
                public int compare(Map<String, String> o1, Map<String, String> o2) {
                    int i = Integer.parseInt(o1.get("index")) - Integer.parseInt( o2.get("index"));
                    return i;
                }
            });

            StringBuffer resBu = new StringBuffer();
            for (int i = 0; i < cityAndIndex.size(); i++) {
                if(i == cityAndIndex.size() - 1){
                    resBu.append(cityAndIndex.get(i).get("city"));
                }else{
                    resBu.append(cityAndIndex.get(i).get("city") + ",");
                }
            }

            if(resBu.toString().indexOf("*") > -1){
                resBu.append("(注:*表示当前该城市存在中风险或高风险地区,并不表示用户实际到访过这些中高风险地区。)");
            }

            return resBu.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

            file.delete();
        }
        return sbf.toString();
    }

    public static void main(String[] arags) {
       System.out.println( readImage("D:/test/1.png","D:/test"));
    }
}

